package com.definesys.smecrailtransit.module.LineReport.adapter;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.base.SmecBaseRecyclerViewHolder;
import com.definesys.smecrailtransit.databinding.ItemLineReportTimeBinding;
import com.definesys.smecrailtransit.module.LineReport.bean.RunTimePerBean;

import java.util.ArrayList;

/**
 * Created by cjy on 2018/1/5.
 */

public class LineReportTimeAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder> {
    private ArrayList<RunTimePerBean> list;
    private DisplayMetrics dm;
    private double maxNumber;

    public  LineReportTimeAdapter(DisplayMetrics dm){
        this.list = new ArrayList<>();
        this.dm = dm;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemLineReportTimeBinding itemLineReportTimeBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_line_report_time,parent,false);
        SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new
                SmecBaseRecyclerViewHolder(itemLineReportTimeBinding.getRoot());
        smecBaseRecyclerViewHolder.setViewType(viewType);
        smecBaseRecyclerViewHolder.setBinding(itemLineReportTimeBinding);
        return smecBaseRecyclerViewHolder;
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, int position) {
        ItemLineReportTimeBinding itemLineReportTimeBinding = (ItemLineReportTimeBinding) holder.getBinding();
        itemLineReportTimeBinding.site.setText(list.get(position).getEleNo());

        if(maxNumber !=0){
            itemLineReportTimeBinding.pillar.getLayoutParams().width = (int)(dm.widthPixels * 1/4 * list.get(position).getValue()/ this.maxNumber);
        }else{
            itemLineReportTimeBinding.pillar.getLayoutParams().width = (int)(dm.widthPixels * 1/4 * list.get(position).getValue());
        }

        itemLineReportTimeBinding.number.setText((int)(list.get(position).getValue()*100)+"%");
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public ArrayList<RunTimePerBean> getList() {
        return list;
    }

    public void setList(ArrayList<RunTimePerBean> list) {
        if(list != null && list.size() != 0){
            this.list = list;

            this.maxNumber = this.list.get(0).getValue();
            for(int i=1 ;i < this.list.size(); i++){
                if(maxNumber < this.list.get(i).getValue()){
                    maxNumber = this.list.get(i).getValue();
                }
            }
        }
    }
}
